package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.HsmanDAO;
import org.jsbd.boss.domian.channel.Hsman;
import org.jsbd.boss.service.IHsmanService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class HsmanServiceImpl implements IHsmanService {
	public static final Logger LOGGER = LoggerFactory.getLogger(HsmanServiceImpl.class);

	@Resource(name = "hmanDao")
	private HsmanDAO hsmanDAO;

	@Resource
	private RedisClient redisClient;

	@Override
	public List<Hsman> selectList(Hsman hsman) {
		String man = hsman.getHsman();
		if (StringUtils.isBlank(man)) {
			hsman.setHsman(null);
		}
		return hsmanDAO.selectList(hsman);
	}

	@Override
	public Integer selectCount(Hsman hsman) {
		return hsmanDAO.selectCount(hsman);
	}

	@Override
	public Hsman selectHsman(Integer id) {
		Hsman hsman = new Hsman();
		hsman.setId(id);
		List<Hsman> list = hsmanDAO.selectList(hsman);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Integer updateHsman(Hsman hsman) {
		return hsmanDAO.updateHsman(hsman);
	}

	@Override
	public Integer insertHsman(Hsman hsman) {
		return hsmanDAO.insertHsman(hsman);
	}

	@Override
	public Integer deleteHsman(Integer id) {
		return hsmanDAO.deleteHsman(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.IHsmanService#batchInsert(java.util.List)
	 */
	@Override
	public void batchInsert(List<Hsman> list) {
		hsmanDAO.batchInsert(list);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IHsmanService#selectListCache(org.jsbd.boss.domian
	 * .channel.Hsman)
	 */
	@Override
	public List<String> selectListCache(String hsman) {
		List<String> hstypeList = (List<String>) redisClient.get(CacheKey.HSMAN_HTYPE_LIST + hsman);
		if (hstypeList != null) {
			return hstypeList;
		}
		hstypeList = new ArrayList<String>();
		Hsman hsman1 = new Hsman();
		hsman1.setHsman(hsman);
		List<Hsman> list = hsmanDAO.selectList(hsman1);
		if (list != null) {
			for (Hsman temp : list) {
				hstypeList.add(temp.getHstype());
			}
			redisClient.put(CacheKey.HSMAN_HTYPE_LIST + hsman, hstypeList);
		}
		return hstypeList;
	}

}
